package com.tfswx.jkgl.designer.datasource;

import com.tfswx.jkgl.designer.dto.xmgl.XmxxQueryOutputDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.List;

/**
 * @author ricky
 * @date 2021/12/14
 */
@Slf4j
@Configuration
public class DataSourceConfiguration {
    // /**
    //  * 交管局数据源
    //  */
    // @Bean(name = "jiaoguanjuDataSource")
    // @Qualifier("jiaoguanjuDataSource")
    // @ConfigurationProperties(prefix = "spring.datasource.jiaoguanju")
    // public DataSource jiaoguanjuDataSource() {
    //     return DataSourceBuilder.create().build();
    // }
    //
    // /**
    //  * 广州数据源
    //  */
    // @Bean(name = "guangzhouDataSource")
    // @Qualifier("guangzhouDataSource")
    // @ConfigurationProperties(prefix = "spring.datasource.guangzhou")
    // public DataSource guangzhouDataSource() {
    //     return DataSourceBuilder.create().build();
    // }
    //
    // /**
    //  * 清远数据源
    //  */
    // @Bean(name = "qingyuanDataSource")
    // @Qualifier("qingyuanDataSource")
    // @ConfigurationProperties(prefix = "spring.datasource.qingyuan")
    // public DataSource qingyuanDataSource() {
    //     return DataSourceBuilder.create().build();
    // }
    //
    // /**
    //  * 韶关数据源
    //  */
    // @Bean(name = "shaoguanDataSource")
    // @Qualifier("shaoguanDataSource")
    // @ConfigurationProperties(prefix = "spring.datasource.shaoguan")
    // public DataSource shaoguanDataSource() {
    //     return DataSourceBuilder.create().build();
    // }
    //
    // /**
    //  * cancl数据源
    //  */
    // @Bean(name = "secondaryDataSource")
    // @Qualifier("secondaryDataSource")
    // @ConfigurationProperties(prefix = "spring.datasource.secondary")
    // public DataSource secondaryDataSource() {
    //     return DataSourceBuilder.create().build();
    // }

    @Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String password;

    private DataSource defaultDataSource;

    @PostConstruct
    void init() {
        defaultDataSource = registDefaultDataSource();
    }

    private final DynamicDataSource dynamicDataSource = new DynamicDataSource();

    public void regist(List<XmxxQueryOutputDTO> xmxxQueryOutputDTOS) {
        dynamicDataSource.myMap.clear();
        dynamicDataSource.sourceMap.clear();
        dynamicDataSource.nameMap.clear();

        loadDefaultDataSource();

        for (XmxxQueryOutputDTO xmxxQueryOutputDTO : xmxxQueryOutputDTOS) {
            String xmbm = xmxxQueryOutputDTO.getXmbm();
            String xmmc = xmxxQueryOutputDTO.getXmmc();
            if (StringUtils.isBlank(xmxxQueryOutputDTO.getSjklx()) ||
                    StringUtils.isBlank(xmxxQueryOutputDTO.getSjklj())) {
                log.info("项目【{}】没有配置数据库", xmxxQueryOutputDTO.getXmmc());
                continue;
            }
            DataSource dataSource = registDataSource(xmxxQueryOutputDTO);
            dynamicDataSource.myMap.put(xmbm, dataSource);
            dynamicDataSource.sourceMap.put(xmbm, dataSource);
            dynamicDataSource.nameMap.put(xmbm, xmmc);
            log.info("加载【{}】数据库({})完成", xmmc, xmbm);
        }
        dynamicDataSource.afterPropertiesSet();
    }

    private DataSource registDefaultDataSource() {
        return DataSourceBuilder.create()
                // .driverClassName("")
                .url(url).username(username).password(password).build();
    }

    private DataSource registDataSource(XmxxQueryOutputDTO xmxx) {
        String className = "";
        String url = "";
        if ("1".equals(xmxx.getSjklx())) {
            // 达梦    192.168.1.19:5236/KJCGZHPT
            url = "jdbc:dm://" + xmxx.getSjklj();
            className = "dm.jdbc.driver.DmDriver";
        } else if ("2".equals(xmxx.getSjklx())) {
            // mysql    192.168.1.26:3306/JKGL
            url = "jdbc:mysql://" + xmxx.getSjklj() + "?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
            className = "com.mysql.cj.jdbc.Driver";
        } else if ("3".equals(xmxx.getSjklx())) {
            // sqlite    ./db/ydtl.db
            url = "jdbc:sqlite:" + xmxx.getSjklj();
            className = "org.sqlite.JDBC";
        } else if ("4".equals(xmxx.getSjklx())) {
            // oracle   127.0.0.1:1521:orcl
            url = "jdbc:oracle:thin:@" + xmxx.getSjklj();
            className = "oracle.jdbc.driver.OracleDriver";
        }
        return DataSourceBuilder.create()
                .driverClassName(className)
                .url(url).username(xmxx.getSjkyhm()).password(xmxx.getSjkmm()).build();
    }

    @Bean(name = "dynamicDataSource")
    @Primary
    public DataSource dynamicDataSource() {
        dynamicDataSource.setTargetDataSources(dynamicDataSource.myMap);//父类的方法
        loadDefaultDataSource();
        dynamicDataSource.setDefaultTargetDataSource(defaultDataSource);//父类的方法
        return dynamicDataSource;
    }

    private void loadDefaultDataSource() {
        dynamicDataSource.sourceMap.put("DEFAULT", defaultDataSource);
        dynamicDataSource.nameMap.put("DEFAULT", "默认数据库");
        log.info("加载【默认数据库】数据库");
    }
}


